﻿using LibVLCSharp.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Utils
{
    /// <summary>
    /// 警报帮助类,
    /// 需要在ubuntu上安装libx11和libvlc
    /// 安装命令如下
    /// sudo apt-get install libx11-dev
    /// sudo apt-get install libvlc-dev
    /// </summary>
    public class AlarmHelp
    {
        
        private static MediaPlayer sp;

        private static string filePath = "";
        private static bool loopingPlay = false;
        private static LibVLC libVlc;
        private static Media media;

        /// <summary>
        /// 加载警报语音
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="LoopingPlay"></param>
        public static void Init(string FilePath,bool LoopingPlay)
        {
            filePath = FilePath;
            libVlc = new LibVLC();
            media = new Media(libVlc, new Uri(FilePath));

            sp = new MediaPlayer(media);
            sp.Stopped += Sp_Stopped;
           
        }

        private static void Sp_Stopped(object sender, EventArgs e)
        {
            sp.Stopped -= Sp_Stopped;
            if (loopingPlay)
            {
                sp = new MediaPlayer(media);
                sp.Stopped += Sp_Stopped;
                sp.Play();
            }
        }

        /// <summary>
        /// 播放音频
        /// </summary>
        public static void Play()
        {
            sp?.Play();
        }

        /// <summary>
        /// 结束播放音频
        /// </summary>
        public static void Stop()
        {
            sp?.Dispose();
        }


    }
}
